package com.study.servlet.servletcontext.resource;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.study.servlet.junit.ReadResourceFileTest;

/**
* @Name: ReadResourceFileDemo03
* @Description: 使用类加载器ClassLoader读取项目资源配置文件
* 特点：
* 	只能加载类路径下（classes目录下）的文件
* @Author: 李超	lc_jack@foxmail.com
* @CreateDate: 2017-3-1 上午10:09:54 
* @Version: V1.0
 */
public class ReadResourceFileDemo03 extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ClassLoader loader = ReadResourceFileDemo03.class.getClassLoader() ;
		InputStream is = loader.getResourceAsStream("db.properties") ;
//		InputStream is = loader.getResourceAsStream("com/study/servlet/servletcontext/db.properties") ;		
		Properties props = new Properties() ;
		try {
			props.load(is) ;
			Enumeration<String> propertyNames = (Enumeration<String>) props.propertyNames() ;
			while(propertyNames.hasMoreElements()) {
				String propertyName = propertyNames.nextElement() ;
				String propertyValue = props.getProperty(propertyName) ;
				System.out.println(propertyName + ":" + propertyValue);
			}
		} catch (Exception e) {
			e.printStackTrace() ;
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}

}
